import React from 'react';
import {
  Form,
  Input,
  Button,
  DatePicker,
  Select,
  Table,
  Icon,
  Radio,
  InputNumber,
  Checkbox,
  Modal,
  message,
  notification,
  Affix,
  Upload,
  Row,
  Col
} from 'antd';
import InnerTable from 'components/DBTable/InnerTable.js';
import InnerTree from './InnerTree.js';


const FormItem = Form.Item;
const ButtonGroup = Button.Group;

/**
 * 内部表格组件
 */
class RoleInnerTable extends InnerTable {

  constructor(props) {
    super(props);
    this.state = { ...this.state, hasValue: false, verification: ''}
  }

  prepareModal = () => {
    return (
      <div>
        <InnerTree ref='innerTree' tableName={this.props.tableName}
          modalState={this.state.modalState}
          modalActionState={this.modalActionState}
          updateId={this.state.updateId}
          parentHandleData={this.handleRoleData}
          callback={this.handleSubmited}
          roleName={this.state.roleName}
          hasValue={this.state.hasValue}
          verification={this.state.verification}
        />
      </div>);
  }

  handleSubmited = (value) => {
    if(value == '') {
      this.setState({ hasValue: false, roleName: value, verification: '请输入角色名称' });
    } else {
      this.setState({ hasValue: true, roleName: value });
    }
  }

  decideHasValue = (value) => {
    if (value === '') {
      this.setState({hasValue: false})
    }else {
      this.setState({hasValue: true})
    }
  }

  handleData = (obj) => {
    if (obj.actionState == this.modalActionState.Insert
      || obj.actionState == this.modalActionState.Update) {
      var data = obj.params;
      data.name = this.refs.innerTree.refs.roleName.refs.input.value;
      data.permissionMap = this.refs.innerTree.getSelectedPermission();
      var body = JSON.stringify(data);
      obj.params = { body: body };

    }
    return obj;
  }
  handleModalOk = () => {
    console.log("哈哈");
    // 将表单中的undefined去掉
    // 表单传过来的主键是逗号分隔的字符串, 这里转换成数组
    if (!this.state.hasValue) {
      this.setState({ verification: '请输入角色名称', });
      return
    }
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        const newObj = {};
        const primaryKeyArray = [];
        const oldObj = this.props.form.getFieldsValue();
        for (const key in oldObj) {
          if (!oldObj[key])
             continue;

          if (key === this.primaryKey && typeof oldObj[key] === 'string') {
            for (const str of oldObj[key].split(', ')) {
              // 按schema中的约定, 主键只能是int/varchar
              if (this.primaryKeyType === 'int') {
                primaryKeyArray.push(parseInt(str));
              } else {
               primaryKeyArray.push(str);
              }
            }
          } else {
            newObj[key] = oldObj[key];
          }
        }

        if (this.primaryKey && primaryKeyArray.length > 0) {
          newObj[this.primaryKey] = primaryKeyArray;
        }

        if (this.state.modalState==this.modalActionState.Update)
        {
           if (this.primaryKey) {
           newObj[this.primaryKey] = this.state.selectedRowKeys[0];
           }
        }


        if (this.state.modalState==this.modalActionState.Insert) {
           this.handleInsert(newObj);
        }
        else if (this.state.modalState==this.modalActionState.Update) {
          this.handleUpdate(newObj);
        }
      }
    });

  }

  // onClickInsert = (e) => {
  //   // console.log("哈哈哈哈点击新增了")
  //   e.preventDefault();

  //   this.handleInsertBefore(this);
  //   this.resetFields();
  //   this.setState({ roleName: '',modalVisible: true, modalTitle: '新增', modalState: this.modalActionState.Insert ,modalForm:false});
  // }
  handleInsertBefore = () => {
    console.log(this.state.roleName);
    if(!!this.state.roleName){
      this.setState({roleName: '', hasValue: true});
    }else {
      this.setState({roleName: '', hasValue: false});
    }
  }
  dealUpdateData = (fieldsValue) => {
    this.setState({roleName: fieldsValue.name, hasValue: true, verification: ''});
  }

}

export default RoleInnerTable;
